﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Globalization;
using Core.Log;

namespace Core.Xml.Ogc.Ows
{
    /// <summary>
    /// Specifies how multiple text values in different languages shall be encoded in 
    /// XML for specific parameters.
    /// 
    /// [cite : OGC 06-121r9, OGC Web Services Common Standard, Version: 2.0.0, 10.7.1]
    /// [uml : OGC 06-121r9, OGC Web Services Common Standard, Version: 2.0.0, Figure 15]
    /// </summary>
    class LanguageStringType : IXmlType
    {
        private string m_Language;
        /// <summary>
        /// Specifies the language (in IETF RFC 4646 syntax) of the string.
        /// </summary>
        public String Language {
            get
            {
                return m_Language;
            }
            set
            {
                // If the Language is not IETF RFC 4646 compliant, log a warning.
                try
                {
                    CultureInfo.GetCultureInfo(value);
                }
                catch (CultureNotFoundException)
                {
                    Logger logger = Logger.GetInstance();
                    logger.Add(new BasicLog(LogPriority.WARNING, "Given language is not valid : " + value));
                }
                finally
                {
                    m_Language = value;
                }
            }
        }

        /// <summary>
        /// Specifies the human-language string..
        /// </summary>
        public String Value { get; set; }

        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        /// <returns>A string that represents the current object.</returns>
        public override string ToString()
        {
            return Value;
        }

        /// <summary>
        /// Creates the XElement of the instance with the given name.
        /// </summary>
        /// <param name="elementName">The name of the XElement</param>
        /// <returns>XElement of the instance with the given name</returns>
        public XElement CreateXElement(XName elementName)
        {
            XElement langString = new XElement(elementName);
            // do not write the xml:lang attribute if null.
            if (!String.IsNullOrEmpty(Language))
                langString.Add(new XAttribute(Xml.Namespaces.XML + "lang", Language));
            langString.Add(new XText(Value));
            return langString;
        }

        /// <summary>
        /// Converts a normal string to a LanguageString implicitly, without any language 
        /// information.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static implicit operator LanguageStringType(string s)
        {
            return new LanguageStringType() { Value = s };
        }
    }
}
